// Copyright (C) joyinter
// All rights reserved.
// 
// Author : dengxinguo
// Date   : 2004-11-21

#include "stdafx.h"
#include ".\MyCoolMenu.h"

// class CCoolMenuItem

CCoolMenuItem::CCoolMenuItem()
{
}

CCoolMenuItem::~CCoolMenuItem()
{
}



void CCoolMenuItem::CM_MouseLeave()
{
    __super::CM_MouseLeave();
}

void CCoolMenuItem::CM_MouseHover()
{
    __super::CM_MouseHover();
    m_listView->SelItem(this);
}

void CCoolMenuItem::CM_Mouse(MouseMessage msg, const CPoint&)
{
    if(msg == MM_LeftUp)
    {
        //GetComboBox()->SelItem(this);
		m_listView->SelItem(this);
    }
}

void CCoolMenuItem::CM_Draw(Graphics &graph )
{

}

// class CCoolMenuList

IMPLEMENT_DYNAMIC(CCoolMenuList, CJoyListView)

CCoolMenuList::CCoolMenuList(void)
{
    m_name = "menu_list";
    m_skinArea = true;
}

CCoolMenuList::~CCoolMenuList(void)
{
}

CCoolMenuList* CCoolMenuList::m_hookList = NULL;
HHOOK CCoolMenuList::m_mouseHook = NULL;

LRESULT CALLBACK CCoolMenuList::MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    LRESULT r = CallNextHookEx(m_mouseHook, nCode, wParam, lParam);
    if(nCode == HC_ACTION)
    {
        MOUSEHOOKSTRUCT* hookInfo = (MOUSEHOOKSTRUCT*)lParam;
        CRect wArea;
        m_hookList->GetWindowRect(&wArea);
        if(!wArea.PtInRect(hookInfo->pt))
        {
            r = 1;

            if(
                (wParam == WM_LBUTTONDOWN) |
                (wParam == WM_LBUTTONUP) |
                (wParam == WM_LBUTTONDBLCLK) |
                (wParam == WM_RBUTTONDOWN) |
                (wParam == WM_RBUTTONUP) |
                (wParam == WM_RBUTTONDBLCLK) |
                (wParam == WM_NCLBUTTONDOWN) |
                (wParam == WM_NCLBUTTONUP) |
                (wParam == WM_NCLBUTTONDBLCLK) |
                (wParam == WM_NCRBUTTONDOWN) |
                (wParam == WM_NCRBUTTONUP) |
                (wParam == WM_NCRBUTTONDBLCLK)
            )
            {
                m_hookList->SetVisible(false);
            }
        }
    }
    return r;
}

BEGIN_MESSAGE_MAP(CCoolMenuList, CJoyListView)
    ON_WM_LBUTTONDOWN()
    ON_WM_SHOWWINDOW()
    ON_WM_MOUSEMOVE()
    ON_WM_ACTIVATE()
    ON_WM_SIZE()
    ON_WM_CLOSE()
END_MESSAGE_MAP()

void CCoolMenuList::OnLButtonDown(UINT nFlags, CPoint point)
{
    CJoyListView::OnLButtonDown(nFlags, point);

}

void CCoolMenuList::OnMouseMove(UINT nFlags, CPoint point)
{
    CJoyListView::OnMouseMove(nFlags, point);

}

void CCoolMenuList::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
    CJoyListView::OnActivate(nState, pWndOther, bMinimized);

    SetVisible(false);
}

void CCoolMenuList::OnSize(UINT nType, int cx, int cy)
{
    CJoyListView::OnSize(nType, cx, cy);

    CRect wr;
    GetWindowRect(&wr);
    CPoint orig(0, 0);
    ClientToScreen(&orig);

    orig.x -= wr.left;
    orig.y -= wr.top;
    HRGN h = CreateRectRgn(orig.x, orig.y, orig.x + cx, orig.y + cy);
    SetWindowRgn(h, TRUE);
}

void CCoolMenuList::OnShowWindow(BOOL bShow, UINT nStatus)
{
    CJoyListView::OnShowWindow(bShow, nStatus);

    if(bShow)
    {
        if(m_mouseHook == NULL)
        {
            m_hookList = this;
            m_mouseHook = SetWindowsHookEx(
                WH_MOUSE, MouseProc, NULL, GetCurrentThreadId());
        }
        SetFocus();
    }
    else
    {
        m_hookList = NULL;
        if(m_mouseHook != NULL)
        {
            UnhookWindowsHookEx(m_mouseHook);
            m_mouseHook = NULL;
        }
    }
}

void CCoolMenuList::OnClose()
{
    SetVisible(false);
    // CJoyListView::OnClose();
}

